package codetop.microsoft.T239;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;

/**
 * @Author: 18362
 * @Create: 2022-08-14 11:08:27 星期日
 */
class Solution {
    public int[] maxSlidingWindow(int[] nums, int k) {
        Deque<Integer> dq = new LinkedList<>();
        for (int i=0; i<k-1; ++i) {
            while (!dq.isEmpty() && nums[dq.getLast()] <= nums[i])
                dq.removeLast();
            dq.addLast(i);
        }
        int[] res = new int[nums.length-k+1];
        for (int i=k-1; i<nums.length; ++i) {
            while (!dq.isEmpty() && nums[dq.getLast()] <= nums[i])
                dq.removeLast();
            dq.addLast(i);
            if (dq.getFirst() < i-k+1)
                dq.removeFirst();
            res[i-k+1] = nums[dq.getFirst()];
        }
        return res;
    }
}